/**
 * 
 */
public class Player {
	Card cardPlayed = null;
	/*
	 * will be set to the current hand held by the player
	 */
    Card[] hand;
    /*
     * if suitPlayedFirst equals 0, it means spade was led
     * if 						 1, it means hearts was led
     * if                        2, it means clubs was led
     * if                        3, it means diamonds was led
     * if                        4, it means no suit has been led
     */
    //cardsPlayed from the field
    Card[] cardsPlayed;
    //bets from the field
    int[] playerBets;
    //number of hands won by everyone on the field
    int[] handsWon;
    int suitPlayedFirst = 0;
    int partnerPos;
    int playingPos;
    /*
     * if player =
     *  false: human player
     *  true:  computer player
     */
    boolean spaded = false;
    boolean hasSpade = false;
   	boolean spadePlayed;
    //Card[] cardsPlayed_, int[] bets_, int[] handsWon_)
    /*
     * only to be used to decide bets, cannot use any other functions!
     * reason it can only be used for bets is because choosing the card to play requires more
     * variables and bets only needs to see the hand
     */
    public Player(Card[] h)
    {
    	hand = h;
    }
    /*
     * DEPRICATED CONSTRUCTOR!
     * use this constructor to get the card the computer should play
     */
    public Player(Card[] h, int suit, boolean s)
    {
    	hand = h;
    	suitPlayedFirst = suit;
    	spaded = s;
    }
    /*
     * Newest Constructor takes into account current board status and plays
     */
    public Player(Card[] hand_, int suitPlayedFirst_, boolean spaded_, Card[] cardsPlayed_, int[] bets_, int[] handsWon_, int partnerPos_, int playingPos_)
    {
    	hand = hand_;
    	suitPlayedFirst = suitPlayedFirst_;
    	spaded = spaded_;
        cardsPlayed = cardsPlayed_;
        //bets from the field
        handsWon = handsWon_;
        partnerPos = partnerPos_;
        playingPos = playingPos_;
    }
    /*
     * returns selected card to play
     */
    public Card getCardToPlay()
    {
    	return cardPlayed;
    }
    /*
     * causes the Player class to determine most optimal card to play, needs some revisions
     * currently it plays the largest card in hand of the suit that led or if the hand lacks
     * that suit it plays the lowest card in hand. current errors include failing to play a
     * spade until the boolean var spaded becomes true
     */
    public void chooseComputerCard()
    {
    	if(suitPlayedFirst==4)
    	{
    		playBiggestCard();
    	}
    	for(int ctr= 0; ctr<13; ctr++)
    	{
    		if(hand[ctr]!=null && hand[ctr].getSuit()==suitPlayedFirst)
    		{
    			cardPlayed = hand[ctr];
    			return;
    		}
    	}
    	if(cardPlayed == null)
    	{
    		Card temp = new Card(14,0);
        	for(int ctr= 0; ctr<13; ctr++)
        	{
        		if(hand[ctr]!=null && hasSpade && hand[ctr].getValue()<=temp.getValue())
        		{
        			cardPlayed = hand[ctr];
        			temp = cardPlayed;
        		}
        		else if(hand[ctr]!=null && !hasSpade && hand[ctr].getValue()<=temp.getValue())
        		{
        			cardPlayed = hand[ctr];
        			temp = cardPlayed;
        		}
        	}
    	}
    	if(cardPlayed == null)
    	{
    		cardPlayed = hand[0];
    	}
    }
	/*
     * returns the largest card in hand and ensures that if spades have not been broken that it does not
     * return the largest spade to start off with
     */
	private void playBiggestCard() {
		Card temp = new Card(0,1);
		for(int ctr = 0; ctr<13; ctr++)
		{
			if(spaded)
				if(hand[ctr]!=null && hand[ctr].getValue()>=temp.getValue())
				{
					temp = hand[ctr];
				}
				else{}
			else
				if(hand[ctr]!=null && hand[ctr].getSuit()!=0 && hand[ctr].getValue()>=temp.getValue())
				{
					temp = hand[ctr];
				}				
		}
		cardPlayed = temp;
	}
	/*
	 * returns lowest card
	 */
	public void playSmallestCard()
	{
		Card temp = new Card(0,13);
		for(int ctr = 0; ctr<13; ctr++)
		{
			if(spaded)
				if(hand[ctr]!=null && hand[ctr].getValue()<=temp.getValue())
				{
					temp = hand[ctr];
				}
				else{}
			else
				if(hand[ctr]!=null && hand[ctr].getSuit()!=0 && hand[ctr].getValue()<=temp.getValue())
				{
					temp = hand[ctr];
				}				
		}
		cardPlayed = temp;
	}
	//used to determine how many hands the computer can win, needs a lot of revision. it is to exclusive
	//in the betting process	
	public int getComputerBet()
	{
		int bet = 0;
		int s = 0;
		for(int ctr = 0; ctr<13; ctr++)
		{
			if(hand[ctr].getValue()==12 || hand[ctr].getValue()==13 || hand[ctr].getValue()==11)
				bet++;
			if(hand[ctr].getSuit()==0)
				s++;
		}
		if(s>=4)
			bet++;
		return bet;
	}
	/*
	 * returns the selected card's position in the hand
	 */
	public int getCardToPlayPos() {
		int c = 0;
		for(int ctr = 0; ctr<13; ctr++)
		{
			if(hand[ctr]!=null && cardPlayed.equals(hand[ctr]))
			{
				c = ctr;
			}
		}
		return c;
	}
	/*
	 * checks if there is a spade in the hand
	 */
	public void hasSpade()
	{
		for(int ctr= 0; ctr<13; ctr++)
    	{
    		if(hand[ctr].getSuit()==0)
    		{
    			hasSpade = true;
    		}
    	}
	}
}
